import 'package:winkai/res/app_colors.dart';
import 'package:winkai/utils/image_utils.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

class EmptyLayout extends StatelessWidget {
  const EmptyLayout({
    super.key,
    required this.type,
    this.hintText,
    this.image,
    this.physics,
    this.size,
    this.loadingIconColor,
    this.onReload,
  });

  final EmptyType type;
  final String? hintText;
  final Widget? image;
  final Size? size;
  final ScrollPhysics? physics;
  final Color? loadingIconColor;
  final void Function()? onReload;

  @override
  Widget build(BuildContext context) {
    List<Widget> widgets = [];
    if (type == EmptyType.loading) {
      widgets.add(CupertinoActivityIndicator(
        radius: 16.0,
        color: loadingIconColor ?? Colors.grey,
      ));
    } else {
      String imageAsset;
      String hint;
      if (type == EmptyType.noNetwork) {
        imageAsset = 'ic_ai_no_network';
        hint = 'no_net_work'.tr;
      } else {
        imageAsset = 'ic_ai_no_data';
        hint = 'no_data'.tr;
      }
      widgets.add(image ?? ImageUtils.getImage(imageAsset, 280.w, 280.w));

      widgets.add(Text(
        hintText ?? hint,
        style: TextStyle(
          fontSize: 14.sp,
          color: AppColors.color_4d,
          fontWeight: FontWeight.w300,
        ),
        textAlign: TextAlign.center,
      ));

      if (type == EmptyType.noNetwork && onReload != null) {
        widgets.add(SizedBox(height: 16.w));
        widgets.add(GestureDetector(
          behavior: HitTestBehavior.translucent,
          child: Container(
            width: 81.w,
            height: 32.w,
            alignment: Alignment.center,
            decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(16.w), color: Colors.black),
            child: Stack(
              alignment: Alignment.center,
              children: [
                Container(
                  width: double.infinity,
                  height: double.infinity,
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(25.w),
                    color: const Color(0xffDFFF32),
                  ),
                ),
                Text(
                  'reload'.tr,
                  style: TextStyle(
                    fontSize: 12.sp,
                    color: Colors.black,
                    fontWeight: FontWeight.w600,
                  ),
                ),
              ],
            ),
          ),
          onTap: () {
            onReload?.call();
          },
        ));
      }
    }

    Widget createContent(double width, double height) {
      if (type != EmptyType.loading && height / width > 1.3) {
        widgets.add(
          SizedBox(height: 150.w),
        );
      }
      return ListView(
        padding: EdgeInsets.zero,
        physics: physics,
        children: [
          SizedBox(
            width: width,
            height: height,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: widgets,
            ),
          )
        ],
      );
    }

    return size != null
        ? createContent(size!.width, size!.height)
        : LayoutBuilder(builder: (context, type) {
            return createContent(type.maxWidth, type.maxHeight);
          });
  }
}

enum EmptyType {
  // 加载中
  loading,
  // 空
  empty,
  //无网络
  noNetwork,
}
